
#include <jni.h>
#include <android/log.h>

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>

#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

#include <binder/IMemory.h>
#include <gui/SurfaceComposerClient.h>

#include <SkImageEncoder.h>
#include <SkBitmap.h>
#include <SkData.h>
#include <SkStream.h>

#include <utils/Log.h>

#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>

#include <binder/IMemory.h>
#include <gui/ISurfaceComposer.h>

#include <SkImageEncoder.h>
#include <SkImageDecoder.h>
#include <SkBitmap.h>

#include <android/native_window.h>
#include <android/native_window_jni.h>

//#include <surfaceflinger/Surface.h>
//#include <surfaceflinger/ISurface.h>
//#include <surfaceflinger/SurfaceComposerClient.h>
#include <gui/Surface.h>


#include "caevent.h"
#include "casock.h"
#include "cathread.h"
#include "calog.h"

#include "CGetPutBuf.h"
#include "renderscr.h"
#include "capturesr.h"


#define CTX (&gCapCtx)
es_t gCapCtx;

using namespace android;

static ScreenshotClient *gSc;
CGetPutBuf *gCapImgBuf;

capconfig_t gCapCfg;

int CapMsgProc(es_t* psys, uint16_t msgid, uint16_t p1, uint32_t p2)
{
#define TIMER_CAP 109

	if(msgid == EM_INIT)
	{
		logprt("== cap msgproc init....\n");
		gSc = new ScreenshotClient();
		gCapImgBuf = new CGetPutBuf(3, SHOT_W*SHOT_H*PIXEL_BYTE);
	}
	else if(msgid == EM_CLOSE)
	{
		logprt("== cap msgproc close......\n");
		es_kill_timer(psys, TIMER_CAP);
		if(gSc)
			free(gSc);
		delete gCapImgBuf;
		gCapImgBuf = NULL;
		
	}
	else if(msgid == EM_TIMER && p1 == TIMER_CAP)
	{
		//logprt("== cap timer expire.....\n");
		void* img;
		img = gCapImgBuf->get_empty(true);
		if(img)
		{
			if(gSc->update(gCapCfg.imgw, gCapCfg.imgh)==NO_ERROR)
			{
				memcpy(img, gSc->getPixels(), gSc->getSize());
				gCapImgBuf->put_fill(img);
			}
		}
		else
		{
			logprt("## no free capture buffer.....\n");
		}
	}
	else if(msgid == UM_ADJ_TIMER)
	{
		logprt("== adj cap timer, timer=%d\n", p1);
		es_set_timer(psys, TIMER_CAP, p1+CAP_PERIOD, NULL);
	}
	else if(msgid == UM_CAPTURE_STOP)
	{
		logprt("== stop capture ....\n");
		es_kill_timer(psys, TIMER_CAP);
	}
	else if(msgid == UM_CAPTURE_START)
	{
		logprt("== start capture ....\n");
		es_set_timer(psys, TIMER_CAP, CAP_PERIOD, NULL);
	}
	return 0;
}

void* capture_thread(void* arg)
{

	es_open(CTX);
	logprt("open cap ctx.....\n");
	es_register_msgproc(CTX, CapMsgProc);
	es_main(CTX);
	es_close(CTX);
	return NULL;
}

pthread_t gtid_cap;
void start_cap_thread(int width, int height, int pixel_byte)
{
	gCapCfg.imgw = width;
	gCapCfg.imgh = height;
	gCapCfg.pixel_byte = pixel_byte;
	logprt("== Capcfg: w=%d, h=%d, pixel_byte=%d\n", width, height, pixel_byte);
	es_thread_start(&gtid_cap, capture_thread, NULL, CTX);
	logprt("    -- cap thread ready....\n");
}

void stop_cap_thread(void)
{
	if(gtid_cap==0)
		return;
	es_exit(CTX);
	es_thread_stop(gtid_cap);
	gtid_cap = 0;
}


void start_capture_samp(void)
{
	logprt("=== send cap samp cmd.\n");
	es_postmsg(CTX, UM_CAPTURE_START, 0, 0);
}

void stop_capture_samp(void)
{
	es_postmsg(CTX, UM_CAPTURE_STOP, 0, 0);
}




void adjust_cap_timer(int break_time)
{
	es_postmsg(CTX, UM_ADJ_TIMER, (uint16_t)break_time, 0);
}

